/**
 * Created by ZhaoJinBing on 2017/8/8.
 */
define(['jquery'], function ($) {
    var createPage = function () {
        $.parser.parse();
        //创建导航树
        $.getJSON('./resource/treeNav', {}, function (data) {
            if (!data) {
                return false;
            }
            $.each(data, function (index, item) {
                var navTreeId = 'navTree_' + index;

                var panel = $('<div style="width:200px;height:auto;padding:10px;"></div>').appendTo('#xpNav');
                panel.after('<br/>');
                panel.panel({
                    title: item.text,
                    content: '<div id="'+navTreeId+'"></div>',
                    collapsible: true
                });

                $('#' + navTreeId).tree({
                    data: item.children,
                    lines: true,
                    onClick: function (node) {
                        var id = node.id;
                        var url = node.attributes['resourceUrl'];
                        var title = node.text;
                        var iconCls = node.iconCls;
                        if (url) {
                            createOrSelTab(id, url, title, iconCls);
                        }
                    }
                });
            });
        });
    };

    //创建或选择tab页
    var createOrSelTab = function (id, url, title, iconCls) {
        var tabPanel = $('#pageTabs');
        if (!tabPanel.tabs('exists',title)) {
            tabPanel.tabs('add',{
                title:title,
                href: url,
                queryParams: {_resourceId: id},
                iconCls:iconCls,
                fit:true,
                closable:true,
                onLoad: function () {
                    require([ 'js/' + url], function (module) {
                        module.init();
                    })
                }
            });
        } else {
            tabPanel.tabs('select', title);
        }
    };

    var getMenuResourceByModuleId = function (id) {
        var resources = [];
        $.ajax({
            url: './resource/childrenResource/' + id,
            type: 'GET',
            dataType: 'JSON',
            async: false,
            success: function (data) {
                resources = data;
            }
        });
        return resources;
    };

    //初始化工具栏
    var initToolBar = function (resources, module) {
        var toolbar = new Array();
        $.each(resources, function (index, item) {
            var e = {};
            var text = item['resourceName'];
            if ("-" == text) {
                toolbar.push("-");
                return true;
            } else {
                e.text = text;
            }

            var iconCls = item['resourceIcon'];
            var target = item['resourceUrl'];
            if (iconCls) {
                e.iconCls = iconCls;
            }
            if (target) {
                e.handler = function () {
                    new Function('module', 'module.' + target + '()')(module);
                }
            }
            toolbar.push(e);
        });
        return toolbar;
    };

    return {
        createPage: createPage
    }
});
